Source Code Viewer - Example 7 - ArithmeticModelDrivenAction

package name.matthewgreet.example7.action;

import org.apache.struts2.ActionSupport;
import org.apache.struts2.ModelDriven;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.interceptor.validation.SkipValidation;

import com.opensymphony.xwork2.validator.annotations.VisitorFieldValidator;

import name.matthewgreet.example7.form.ArithmeticForm;



/**
 * Tests Struts 2 automatic type conversion for integers. 
 */
//@InterceptorRef(value="defaultStack", params={"conversionError.excludeMethods", "submit"})
@InterceptorRef("defaultStack")
@Results({
	@Result(name=ActionSupport.ERROR, location="/pages/ArithmeticModelDriven.jsp"),
	@Result(name=ActionSupport.INPUT, location="/pages/ArithmeticModelDriven.jsp"),
	@Result(name=ActionSupport.SUCCESS, location="/pages/ArithmeticModelDriven.jsp")
})
public class ArithmeticModelDrivenAction extends ActionSupport implements ModelDriven<ArithmeticForm> {
	private static final long serialVersionUID = -6084392315736424463L;
	
	private ArithmeticForm form;

	private String resultText;
	
	
	@Override
	@VisitorFieldValidator(appendPrefix = false)
	public ArithmeticForm getModel() {
        if (form == null) {
            form = new ArithmeticForm();
        }
        return form;
	}

    @Action("/ArithmeticModelDriven")
    @Override
    @SkipValidation
    public String execute() throws Exception {
		try {
			int result;
			
			if (form == null) {
				resultText = "";
			} else if (form.getOperand2() != null) {
				switch (form.getOperator()) {
				case "+":
					result = form.getOperand1() + form.getOperand2();
					break;
				case "-":
					result = form.getOperand1() - form.getOperand2();
					break;
				case "/":
					result = form.getOperand1() / form.getOperand2();
					break;
				case "*":
					result = form.getOperand1() * form.getOperand2();
					break;
				default:
					result = 0;
					break;
				}
				
				resultText = form.getOperand1() + " " + form.getOperator() + " " + form.getOperand2() + " = " + result;
			} else {
				resultText = "Operand 2 not set";
			}
			
			return SUCCESS;
		}
		catch (Exception e) {
			System.out.println("Action failed  reason=" + e.getMessage());
			return ERROR;
		}
    }
	
    @Action("/ArithmeticModelDriven_submit")
    public String submit() throws Exception {
		try {
			int result;
			
			if (form.getOperand2() != null) {
				switch (form.getOperator()) {
				case "+":
					result = form.getOperand1() + form.getOperand2();
					break;
				case "-":
					result = form.getOperand1() - form.getOperand2();
					break;
				case "/":
					result = form.getOperand1() / form.getOperand2();
					break;
				case "*":
					result = form.getOperand1() + form.getOperand2();
					break;
				default:
					result = 0;
					break;
				}
				
				resultText = form.getOperand1() + " " + form.getOperator() + " " + form.getOperand2() + " = " + result;
			} else {
				resultText = "Operand 2 not set";
			}
			
			return SUCCESS;
		}
		catch (Exception e) {
			System.out.println("Action failed  reason=" + e.getMessage());
			return ERROR;
		}
    }
	

    
    
    public ArithmeticForm getForm() {
		return form;
	}

	public String getResultText() {
		return resultText;
	}

	

}